/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mercury3;

import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxConnectionConstraint;
import com.mxgraph.view.mxGraph;
import com.mxgraph.view.mxStylesheet;
import com.sun.javafx.geom.Edge;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import javafx.scene.control.Cell;
import javax.swing.JPanel;
import mercury3.editor.BasicGraphEditor;
import mercury3.editor.EditorPopupMenu;

/**
 *
 * @author jucem_000
 */
public class Grafo {

    mxGraph graph;
    Object parent;
    String label = new String("label");
    int contador = 0;
    private boolean select;

    public Grafo(JPanel painel) {
        graph = new mxGraph();
        graph.getDefaultParent();

        //configurações do grafo
        graph.setEdgeLabelsMovable(true);//habilita mover labels edge
        graph.setVertexLabelsMovable(true);//habilita mover labels celula
        graph.setAllowDanglingEdges(false);//desabilita desconectar edge
        graph.setCellsMovable(true);//habilita mover celula
        graph.setCellsResizable(true);//desabilita editar celular

        //graph.setCellsEditable(true);
        applyEdgeDefaults();
        applyVertexDefauts();

        mxGraphComponent graphComponent = new mxGraphComponent(graph);
        graphComponent.setPreferredSize(new Dimension(800, 600));

        graphComponent.setConnectable(true);//habilita criar linhas
        graphComponent.getViewport().setOpaque(true);//muda o fundo

        painel.add(graphComponent);
        eventMouse(graphComponent);

    }

    private void eventMouse(mxGraphComponent graphComponent) {
        graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {

            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {

                    graph.getModel().beginUpdate();
                    try {
                        graph.insertVertex(parent, null, label + contador, e.getX() - 25, e.getY() - 25, 50, 50);
                    } finally {
                        graph.getModel().endUpdate();
                        contador++;
                    }
                }
                if (e.getButton() == MouseEvent.BUTTON3) {
                    //EditorPopupMenu popup = new EditorPopupMenu(editor);
                }
            }

            public void mouseReleased(MouseEvent e) {

            }

            public void mousePressed(MouseEvent e) {

            }
        });
    }

    private void applyEdgeDefaults() {
        // Settings for edges
        Map<String, Object> edge = new HashMap<String, Object>();
        edge.put(mxConstants.STYLE_ROUNDED, true);
        edge.put(mxConstants.STYLE_ORTHOGONAL, false);
        edge.put(mxConstants.STYLE_EDGE, "elbowEdgeStyle");
        edge.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_CONNECTOR);
        edge.put(mxConstants.STYLE_ENDARROW, mxConstants.ARROW_CLASSIC);
        edge.put(mxConstants.STYLE_VERTICAL_ALIGN, mxConstants.ALIGN_BOTTOM);//ALIGN_MIDDLE);
        edge.put(mxConstants.STYLE_ALIGN, mxConstants.ALIGN_CENTER);
        edge.put(mxConstants.STYLE_STROKEWIDTH, 2);
        edge.put(mxConstants.STYLE_STROKECOLOR, "#000000"); // default is #6482B9
        edge.put(mxConstants.STYLE_FONTCOLOR, "#000000");

        mxStylesheet edgeStyle = new mxStylesheet();
        edgeStyle.setDefaultEdgeStyle(edge);
        graph.setStylesheet(edgeStyle);
    }

    private void applyVertexDefauts() {
        Hashtable<String, Object> style = new Hashtable<String, Object>();
        style.put(mxConstants.STYLE_FILLCOLOR, mxConstants.NONE);//mxUtils.getHexColorString(new Color(255, 255, 255)));//"#C3D9FF");
        style.put(mxConstants.STYLE_STROKEWIDTH, 3);
        style.put(mxConstants.STYLE_STROKECOLOR, mxUtils.getHexColorString(new Color(0, 0, 0)));
        style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
        style.put(mxConstants.STYLE_PERIMETER, mxConstants.PERIMETER_ELLIPSE);
        style.put(mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_BOTTOM);
        style.put(mxConstants.STYLE_VERTICAL_LABEL_POSITION, mxConstants.ALIGN_TOP);

        mxStylesheet stylesheet = graph.getStylesheet();
        stylesheet.setDefaultVertexStyle(style);
        graph.setStylesheet(stylesheet);
    }

    void lerGrafo() {
        Object[] edges;
        Object[] cells;
        mxConnectionConstraint[] conect;

        cells = graph.getChildVertices(graph.getDefaultParent());
        edges = graph.getChildEdges(graph.getDefaultParent());
        //graph.getAllEdges(cells);
        
        System.out.println("markov RedundantGC{");
        for (int i = 0; i < cells.length; i++) {        
            if (cells[i] instanceof mxCell) {
                mxCell cell = (mxCell) cells[i];
                System.out.println("state " + cell.getValue()+ ";");
            }
        }
        System.out.println("");
        for (int i = 0; i < edges.length; i++) {
            if (edges[i] instanceof mxCell) {
                mxCell edge = (mxCell) edges[i];
                System.out.println("transation " + edge.getSource().getValue() +
                        " -> " +edge.getTarget().getValue() + " (rate = " + edge.getValue() + ");");
            }
            
        }
        System.out.println("}");
    }

}
